package com.notes.config.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.MessageFormat;

/**
 * @author huxingxin
 * 项目启动 打印swagger文档地址
 */
@Component
@Slf4j
public class ApplicationApiServer implements ApplicationListener<WebServerInitializedEvent> {
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        String host = null;
        try {
            host = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        int port = event.getWebServer().getPort();
        String url = MessageFormat.format("http://{0}:{1}", host, String.valueOf(port));

        log.info("接口文档地址：{}/doc.html", url);
    }
}
